const axios = require('axios');
const Q = require('q');
const menudata = require( 'api/menus');       //  接口js引用
/*axios.defaults.withCredentials = true;*/
/*axios.defaults.crossDomain = true;*/
/**
// * Mocking client-server processing
 */
module.exports = {

    // 商品新增功能 添加显示和隐藏公共变量
    specificationsNum:true,

    heads :'',
    /***  菜单模块  ***/
    /***  登录模块  ***/
    loginUrl :'http://shanghui/api/users/login',
    loginData:function(params) {
        var deferred = Q.defer();
        axios({
            method:'get',
            url:this.loginUrl,
            params:params
        }).then( (res) => {
            this.heads = "Bearer "+res.data.data.Authorization;

            deferred.resolve(res.data);
        });
        return deferred.promise;
    },
    //  图片库
    //  图片添加
    /*uploadPicture :'http://shanghui/api/uploadPicture',*/
    uploadPicture:'http://10.0.0.10:7788/api/customer/increaseVehicle',
    uploadPictureAdd:function (params) {
        console.log(params)
        var data = new FormData();
        data.append('license', params);
        data.append('licenses', params);
        var config = {
            headers:{
                Authorization:this.heads,
                /*'content-type': 'multipart/form-data',*/
                'content-type': 'application/json; charset=UTF-8'
            },
            onUploadProgress: function(progressEvent) {
                var percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
            }
        };
        var deferred = Q.defer(); axios({
            method:'post',
            url:this.uploadPicture,
            data,
            onUploadProgress: function(progressEvent) {
                var percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
            }
        }).then( (res) => {
            deferred.resolve(res.data);
        });
        return deferred.promise;
    },
    /***  新闻模块  ***/
    newsUrl :'http://shanghui/api/news',
    //  查询方法
    newsList:function(params){
        var deferred = Q.defer(); axios({
            method:'get',
            url:this.newsUrl,
            headers:{Authorization:this.heads},
            params:params
        }).then( (res) => {deferred.resolve(res.data);});
        return deferred.promise;},
    //  删除方法
    newsDel:function(params) {
        var deferred = Q.defer(); axios({
            method:'delete'
            ,url:this.newsUrl,
            headers:{Authorization:this.heads},
            params:params
        }).then( (response) => { deferred.resolve(response);});
        return deferred.promise;
    },
    //  新增方法
    newsAdd:function(params) {
        var deferred = Q.defer(); axios({
            method:'post',
            url:this.newsUrl,
            headers:{Authorization:this.heads},
            params:params
        }).then( (res) => {deferred.resolve(res.data);});
        return deferred.promise;
    },
    //  修改方法
    newsUpd:function(params) {
        var deferred = Q.defer(); axios({
            method:'patch',
            url:this.newsUrl,
            headers:{Authorization:this.heads},
            params:params
        }).then( (res) => {
            deferred.resolve(res.data);
        });
        return deferred.promise;
    },


    urls:function(){
        var deferred = Q.defer();
        axios({
            method:'POST',
            url: 'http://10.0.0.12/ins/gift/obtainGiftClassifyList',
            headers:{
                'Content-Type':'application/x-www-form-urlencoded'
            },
            params : {
                page:1,
                size:10
            },
            /* withCredentials:true   //加了这段就可以跨域了*/
        }).then(function (response) {
            deferred.resolve(response.data);
        }).catch(function (error) {
            console.log(error)
        });
        return deferred.promise;
    },



}

